<template>
  <div class="goods">
    <div class="wrap">
      <!-- 导航信息!!!!! -->
      <Crumb> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span
            :class="{ active: index == options1Active }"
            v-for="(item, index) in options1"
            :key="index"
            @click="options1Fn(index, item.min, item.max)"
            >{{ item.txt }}</span
          >
          <!-- 
              点击排序项 ， 得到当前min ，max 积分值，执行查询商品的方法
             {显示内容：‘500-1000’，min:500,max:1000}
           -->
        </li>
        <li>
          <strong>分类：</strong>
          <span
            :class="num2 == index ? 'active' : ''"
            v-for="(item, index) in options2"
            :key="index"
            @click="options2Fn(index, item.type)"
            >{{ item.txt }}</span
          >
        </li>
      </ul>
      <List :arr="goodsListShow" />
       <div style="text-align: center; margin-top: 20px">
        {{ isReachBottom ? "已经没有数据了" : "正在加载..." }}
      </div>
    </div>
  </div>
</template>                            
<script>
import { getScrollTop, getClientHeight, getScrollHeight } from "@/utils";
import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
import { GoodsSearchAPI } from "@/request/api";
export default {
  data() {
    return {
      //栏目  0全部  1 精品 2 热门
      did: 0,
      //类型  0全部 1实体 2 虚拟
      type: 0,
      //最小积分值
      min: 0,
      //最大积分值   值为0 与逻辑无关
      max: 0,
      //搜索关键字
      keyword: "",
      //商品arr
      goodsList: [],
      //排序项数组
      options1: [
        { min: 0, max: 10000, txt: "全部" },
        // "我还可以兑换"的max值是Header组件的coin值
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500分" },
        { min: 500, max: 1000, txt: "500-1000分" },
        { min: 1000, max: 1500, txt: "1000-1500分" },
        { min: 1500, max: 2500, txt: "1500-2500分" },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      options1Active: 0,
      options2: [
        { type: 0, txt: "全部" },
        { type: 1, txt: "实体商品" },
        { type: 2, txt: "虚拟商品" },
      ],
      //排序当前项
      num1: 0,
      //分类当前项目
      num2: 0,
      goodsListShow: [],
      page: 1,
      size: 8,
      //是否加载数据
      isLoading: false,
      isReachBottom: false,

    };
  },
  components: {
    Crumb,
    List,
  },
  created() {
    this.goodsSearch();
  },
  mounted() {
    window.addEventListener("scroll", this.scrollFn);
  },
  beforeDestroy() {
    window.removeEventListener("scroll", this.scrollFn);
  },
  methods: {
    async goodsSearch() {
      let res = await GoodsSearchAPI({
        did: this.did,
        type: this.type,
        min: this.min,
        max: this.max,
        keyword: this.keyword,
      }).then((res) => {
        if (res.code === 0) {
          this.goodsList = res.data;
          //默认展示第一页 8
          this.goodsListShow = this.goodsList.filter(
            (item, index) => index < 8
          );
          //初始化
          this.page = 1;
          this.isReachBottom = false;
          //数据是否到达底部
          if (this.goodsListShow.length == this.goodsList.length) {
            this.isReachBottom = true;
            return;
          }
        }
      });
    },
    options1Fn(idx, min, max) {
      this.options1Active = idx;
      console.log(min, max);
      //更新查询条件
      this.min = min;
      this.max = max;
      //重新查询
      this.goodsSearch();
    },
    options2Fn(index, type) {
      this.num2 = index;
      this.type = type;
      //重新查询
      this.goodsSearch();
    },
    scrollFn() {
      // console.log(
      //   "滚动了",
      //   getScrollTop() + getClientHeight(),
      //   getScrollHeight()
      // );
      if (getClientHeight() + getScrollTop() >= getScrollHeight() - 300) {
        //数据是否到达底部
        if (this.goodsListShow.length == this.goodsList.length) {
          this.isReachBottom = true;
          return;
        }
        //当前滚动快时,会频繁执行scrollFn,触发this.page++,一下子加载了更多
        //优化:通过一个变量让我们的代码时间内不被重复执行
        //this.isLoading 是否正在加载数据
        if (this.isLoading == false) {
          //一进来就是加载数据中...
          this.isLoading = true;
          this.page++;
          console.log("可以加载下一页数据", this.page);
          setTimeout(() => {
            for (
              let i = (this.page - 1) * this.size;
              i < this.page * this.size;
              i++
            ) {
              console.log("for", this.page, i, this.goodsList[i]);
              this.goodsList[i] && this.goodsListShow.push(this.goodsList[i]);
              //bug 分析为什么 再思考如何解决
            }
            this.isLoading = false;
          }, 1000);
        }

        //思考?如何拿到下一页数据 并显示到页面中
        //前提:页面已经显示了 第一页 每页显示8条数据
        //方式一:传递第二页,8条数据 调用后台接口 返回给我们
        //方式二:作goodList数组中拿第二页的数据,把数据从goodList push到 goodsList
        // page:1  size:8  idnex:0~7 数据 created()
        // page:2  size:8  idnex:8~15
        // page:3  size:8  idnex:16~23
      }
    },
  },
};
</script>
 
<style lang = "less" scoped>
@import "../assets/css/less.css";
@base-color: #0a328e;
.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
}
</style>